package com.crazyxmq.linear;

import java.util.Iterator;

public class Stack<T> implements Iterable<T> {


    // 记录首节点
    private Node head;
    // 栈中元素的个数
    private int N;

    private class Node{
        public T item;
        public Node next;

        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }

    public Stack() {
        this.head = new Node(null,null);
        this.N = 0;

    }
    // 判断当前栈中元素个数是否为零
    public boolean isEmpty(){
        return N==0;
    }
    // 获取栈中元素个数
    public int size(){
        return N;
    }

    // 压栈
    public void push(T t){
        // 找到首结点指向的第一个结点
        Node oldFirst = head.next;
        Node newNode = new Node(t, null);
        head.next = newNode;
        newNode.next = oldFirst;
        N++;
    }
    // 出栈
    public T pop(){
        //找到首结点指向的第一个结点
        Node oldFirst = head.next;
        if(oldFirst == null){
            return null;
        }
        // 让首结点指向原来第一个结点的下一个结点
        head.next = oldFirst.next;
        // 元素个数减 1
        N--;

        return oldFirst.item;
    }

    @Override
    public Iterator iterator() {
        return new SIterator();
    }

    private class SIterator implements Iterator{

        private Node n;

        public SIterator(){
            this.n = head;
        }

        @Override
        public boolean hasNext() {
            return n.next!= null;
        }

        @Override
        public Object next() {
            n = n.next;
            return n.item;
        }
    }
}
